;(function(Ext,metaData){
	Ext.Loader.setPath("FormFieldUtil",contextPath+"ui_tpl/FormFieldUtil.js");
	var 
	refMetaMap=metaData["refMetaMap"],
	refEnumMap=metaData["refEnumMap"],
	mainMetaId=metaData['id'],
	mainMeta=refMetaMap[mainMetaId],
	attrs=mainMeta["attrs"],
	loadRecordUrl=attrs["loadRecordUrl"]||(contextPath+"meta/singleRecord/"+metaData["id"]),
	fields=mainMeta["fields"],
	modelFields=[],mainView="Ext.ux.SingleForm",
	typeMap={"1":"string","-5":"int","4":"int","12":"string","16":"boolean","91":"date","93":"date","97":"date"};
	Ext.iterate(fields,function(field,i){
		
		var type=field["type"],name=field["name"],f={name:name,type:typeMap[field["type"]]},
		valid={};
		
		modelFields.push(f);
	});
	modelName="Ext.ux."+mainMeta["code"];
	
	Ext.define(modelName,{
		extend:"Ext.data.Model",
		idProperty:attrs["idProperty"]||"id",
		fields:modelFields
	});
	Ext.define(mainView,{
		extend:'Ext.form.Panel',
		xtype:'singleform',
		bodyPadding: 15,
		scrollable:true,
		requires:[
			'FormFieldUtil'
		],
		layout: 'anchor',
		defaults: {
		        anchor: '100%'
		 },
		defaultType: 'textfield',
		initComponent:function(){
			var me=this,attrs=this.attrs=mainMeta["attrs"],
			initConfig=me.initialConfig;
			this.idProperty=this.attrs["idProperty"];
			this.action=this.attrs["action"];
			this.url=this.action["url"];
			me.initButtons();
			me.items=FormFieldUtil.getFormFields(mainMetaId,metaData);
			this.callParent();
		},
		listeners:{
			'afterrender':function(){
				var me=this,params={};
				if(globalId){
					params={id:globalId};
				}
				Ext.Ajax.request({
					url:loadRecordUrl,
					method:"GET",
					params:params,
					success: function(response, opts) {
				         var obj = Ext.decode(response.responseText);
				         if(obj["success"]){
				        	 var data=obj["data"];
				        	 var rec=Ext.create(modelName,data);
				        	 me.loadRecord(rec);
				         }
				     },

				     failure: function(response, opts) {
				    	 Ext.toast('提示','数据加载失败');
				     }
				});
			}
		},
		initButtons:function(){
			this.buttons=[{
		        text: '重置',
		        iconCls:"fa fa-refresh fa-fw",
		        handler: function() {
		            this.up('form').getForm().reset();
		        }
		    }, {
		        text: '提交',
		        iconCls:"fa fa-arrow-up",
		        formBind: true,
		        disabled: true,
		        handler: function() {
		            var form = this.up('form').getForm();
		            if (form.isValid()) {
		                form.submit({
		                    success: function(form, action) {
		                    	Ext.toast("提交成功",'提示', 't');
		                    	
		                    },
		                    failure: function(form, action) {
		                    	Ext.toast(action.result.message,'提示', 't');
		                    }
		                });
		            }
		        }
		    }];
		}
		
	});
	
	Ext.application({
	    name: 'MyApp',
	    mainView:mainView,
	    launch: function () {
	    }
	});
})(Ext,metaData);

